<?php

namespace App\Imports;

use App\Events\ImportPersonEvent;
use App\Helpers\ResponseData;
use App\Models\Employee;
use App\Models\Important\ImportantTag;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithValidation;
use App\Models\Important\ImportantPerson;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
class ShortCut implements ToCollection,WithChunkReading,WithBatchInserts,WithValidation,WithStartRow
{
    protected $start = 4;//第几行开始导入
    protected $size = 100;//分块大小
    protected $chunk = 0;//第几块
    protected $shop_id = 0;
    public function __construct($id){
        $this->shop_id = $id;
    }
    public function startRow(): int
    {
        return 2;
    }

    /**
     * @return int
     */
    public function chunkSize(): int
    {
        return $this->size;
    }
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function collection(Collection $rows)
    {
        DB::transaction(function () use($rows){
            foreach ($rows as $key => $row) {
                $data = [
                    'group_id' => $row[0],
                    'code' => $row[1],
                    'content' => $row[2],
                    'shop_id'=>$this->shop_id,
                ];
                \App\Models\Shortcut::query()->create($data);
            }
        });

    }
    public function batchSize(): int
    {
        return 1000;
    }

    public function rules(): array
    {
        return [
//            '4'=>function($attribute, $value, $onFailure) {
//               if(!preg_match("/^1[34578]\d{9}$/", $value)){
//                   return response()->json('手机号不匹配');
//               }
////                if ($value !== 'Patrick Brouwers') {
////                    $onFailure('Name is not Patrick Brouwers');
////                }
//            }

        ];
    }
}
